# -*- coding: utf-8 -*-
"""
Created on Tue Feb 17 16:40:25 2015

@author: DELPINO
"""

# -*- coding: utf-8 -*-
"""
Created on Wed Feb 04 17:30:41 2015

@author: DELPINO
"""
import httplib2
import pprint

from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run


# Copy your credentials from the console
CLIENT_ID = '403447858536-l96sef991ttlalkfl7m2bea979krsmg7.apps.googleusercontent.com'
CLIENT_SECRET = 'YCpjE0hP7l3fjhLie8dljL_H'
SCOPE = 'https://www.googleapis.com/auth/drive'


# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

# Path to the file to upload
FILENAME = 'document.txt'

# Run through the OAuth flow and retrieve credentials
#flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE,
#                           redirect_uri=REDIRECT_URI)
flow = flow_from_clientsecrets('client_secrets.json',
                               scope=SCOPE,
                               redirect_uri='http://localhost')

storage = Storage('plus.dat')
credentials = storage.get()                               
   
if credentials is None or credentials.invalid:
    credentials = run(flow, storage)   
                          
http = httplib2.Http()
http = credentials.authorize(http)

drive_service = build('drive', 'v2', http=http)

# Insert a file
media_body = MediaFileUpload(FILENAME, mimetype='text/plain', resumable=True)
body = {
  'title': 'My document',
  'description': 'A test document',
  'mimeType': 'text/plain'
}

file = drive_service.files().insert(body=body, media_body=media_body).execute()

pprint.pprint(file)
